package cn.com.ylpw.web.order.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;

import cn.com.ylpw.batis.MyBatisFactory;
import cn.com.ylpw.web.order.service.IAddressService;
import cn.com.ylpw.web.util.AbstractService;
@Service("addressService")
public class AddressServiceImpl extends AbstractService implements IAddressService {
	private final static String NAME_SPACE = "cn.com.ylpw.web.order.Address";
	@Override
	public List<Map<String, Object>> findAllProvinces(Map<String, Object> params) throws Exception {
		return super.superDao.getMyBatisFactory().findObjectList(NAME_SPACE, "getAllProvince", params);
	}
	@Override
	public List<Map<String, Object>> findAllCitys(Map<String, Object> params) throws Exception {
		return super.superDao.getMyBatisFactory().findObjectList(NAME_SPACE, "getAllCity", params);
	}
	@Override
	public List<Map<String, Object>> findAllAreas(Map<String, Object> params) throws Exception {
		return super.superDao.getMyBatisFactory().findObjectList(NAME_SPACE, "getAllArea", params);
	}
	@Override
	public List<Map<String, Object>> findAllCodes(Map<String, Object> params) throws Exception {
		return super.superDao.getMyBatisFactory().findObjectList(NAME_SPACE, "getAllCode", params);
	}
	@Override
	public Map<String, Object> findProvinceById(Map<String, Object> params) throws Exception {
		return super.superDao.getMyBatisFactory().getObject(NAME_SPACE, "getProvince", params);
	}
	@Override
	public Map<String, Object> findCityById(Map<String, Object> params) throws Exception {
		return super.superDao.getMyBatisFactory().getObject(NAME_SPACE, "getCity", params);
	}
	@Override
	public Map<String, Object> findAreaById(Map<String, Object> params) throws Exception {
		return super.superDao.getMyBatisFactory().getObject(NAME_SPACE, "getArea", params);
	}
	@Override
	public List<Map<String, Object>> findCodeNameIsNotNull(Map<String, Object> params) {
		return super.superDao.getMyBatisFactory().findObjectList(NAME_SPACE, "getCodeNameIsNotNull", params);
	}	
	@Override
	public List<Map<String, Object>> findCodeNameIsNull(Map<String, Object> params) {
		return super.superDao.getMyBatisFactory().findObjectList(NAME_SPACE, "getCodeNameIsNull", params);
	}
	@Override
	public Map<String, Object> findAddressCustomerById( Map<String, Object> params) {
		return super.superDao.getMyBatisFactory().getObject(NAME_SPACE, "getAddressCustomerById", params);
	}
	@Override
	public Map<String, Object> findCodeById(Map<String, Object> params) {
		return super.superDao.getMyBatisFactory().getObject(NAME_SPACE, "getCode", params);
	}
	@Override
	public Map<String, Object> findTexpressFeespecial(Map<String, Object> params) {
		return super.superDao.getMyBatisFactory().getObject(NAME_SPACE, "getTexpressFeespecial", params);
	}
	@Override
	public int saveCustomerAddr(Map<String, Object> params) {
		MyBatisFactory factory = super.superDao.getMyBatisFactory();
		if(params.get("orders").toString().equals("1")){
			Map<String,Object> _params = Maps.newHashMap();
			_params.put("customersId", params.get("customersId"));
			_params.put("orders",0);
			factory.updateObject(NAME_SPACE, "updateCustomerAddrOrders", _params);
		}
		return factory.insertObject(NAME_SPACE, "saveCustomerAddr", params);
	}
	@Override
	public int updateCustomerAddr(Map<String, Object> params) {
		MyBatisFactory factory = super.superDao.getMyBatisFactory();
		if(params.get("orders").toString().equals("1")){
			Map<String,Object> _params = Maps.newHashMap();
			_params.put("customersId", params.get("customersId"));
			_params.put("orders",0);
			factory.updateObject(NAME_SPACE, "updateCustomerAddrOrders", _params);
		}
		return factory.updateObject(NAME_SPACE, "updateCustomerAddr", params);
	}
	@Override
	public int setAddressByDefault(Map<String, Object> params) {
		MyBatisFactory factory = super.superDao.getMyBatisFactory();
		Map<String,Object> _params = Maps.newHashMap();
		_params.put("customersId", params.get("customersId"));
		_params.put("orders",0);
		factory.updateObject(NAME_SPACE, "updateCustomerAddrOrders", _params);
		factory.updateObject(NAME_SPACE, "updateCustomerAddrOrders", params);
		return 0;
	}	
	
}
